import os, time, random
from multiprocessing import  Process, Pool, Queue
from gevent import  monkey
monkey.patch_all()
import gevent

from  data.DataStore import store_data

def startTest(queue ):
    test = TestQuen(queue)
    test.crawl()

class TestQuen(object):

    def __init__(self, queue):
        self.queue = queue

    def run_proc(self, name):
        print('Run child process %s (%s)....' % (name, os.getpid()))

    def long_time_task(self, name):
        print('Run task %s (%s)...' % (name, os.getpid()))
        start = time.time()
        time.sleep(random.random() * 3)
        end = time.time()
        print('Task %s runs %0.2f seconds.' % (name, (end -start)))
    def crawl1(self, p):
        while True:
            if self.queue.full():
                print('等待')
                time.sleep(0.1)
            else:
                self.queue.put(p)
                break
    def crawl(self):
        i = 0
        while True:
            spawns = []
            i += 100;
            for p in range(0, i):
                spawns.append(gevent.spawn(self.crawl1, p))
                if len(spawns) >= 3:
                    gevent.joinall(spawns)
                    spawns = []
            gevent.joinall(spawns)

if __name__ == '__main__':
    print('parent process %s.' % os.getpid())
    # p = Process(target=run_proc, args=('test', ))
    # print('Child process will start.')
    # p.start()
    # p.join()
    # print('Child process end.')

    #进程池
    # p = Pool(4)
    # for i in  range(5):
    #     p.apply_async(long_time_task, args=(i,))
    # print('Waiting for all subprocesses done...')
    # p.close()
    # p.join()
    # print('All subprocesses done.')

    q = Queue()
    p = Process(target=startTest, args=(q,))
    p1 = Process(target=store_data, args=(q,))
    p.start()
    p1.start()
